package com.sinodata.bsm.cicp.collector.jboss;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:38:44          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JbossCollector extends Collector {
    private static Logger logger = Logger.getLogger(JbossCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        try {
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            ObjectName jbossServer = new ObjectName("jboss.system:type=Server");
            ObjectName jbossServerInfo = new ObjectName("jboss.system:type=ServerInfo");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = (Date) rmiAdapter.getAttribute(jbossServer, "StartDate");
            String startTime = sdf.format(startDate);
            String activeThread = rmiAdapter.getAttribute(jbossServerInfo, "ActiveThreadCount") + "";
            String activeThreadGroup = rmiAdapter.getAttribute(jbossServerInfo, "ActiveThreadGroupCount") + "";
            String maxMemSize = (Long) rmiAdapter.getAttribute(jbossServerInfo, "MaxMemory") / 1024 / 1024 + "";
            String totalMemSize = (Long) rmiAdapter.getAttribute(jbossServerInfo, "TotalMemory") / 1024 / 1024 + "";
            String freeMemSize = (Long) rmiAdapter.getAttribute(jbossServerInfo, "FreeMemory") / 1024 / 1024 + "";

            propValues[0] = ResPropertyValue.createPropValue(startTime, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(activeThread, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(activeThreadGroup, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(maxMemSize, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(totalMemSize, propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue(freeMemSize, propMetas[5]);

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "服务启动时间", "活动线程数量", "活动线程组数量", "最大内存", "内存总量", "空闲内存" };
        //        return new String[] { "ServiceStartTime", "ActiveThreadsNum", "ActiveThreadGroupsNum", "MaxMemory", "TotalMemorySize", "IdleMemory" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };

    }
}
